/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.


See LICENSE and AUTHORS for more information.
*/

#include "connecttocoredialog.h"
#include <QSettings>
#include <QDebug>

/**
* @brief setup the ui and connect signals
*/
ConnectToCoreDialog::ConnectToCoreDialog()
{
    form = new Ui_ConnectToCoreDialog();
    form->setupUi(this);
    connect(this->form->connectButton,SIGNAL(clicked()),this,SLOT(doConnect()));
    connect(this->form->accountList,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(itemChanged(QListWidgetItem*)));
    connect(this->form->accountList,SIGNAL(itemSelectionChanged()),this,SLOT(selectionChanged()));
    this->form->passwordEdit->setEchoMode(QLineEdit::Password);
    this->form->accountList->setSelectionMode(QAbstractItemView::SingleSelection);
    this->form->accountList->setEditTriggers(QAbstractItemView::DoubleClicked);

    this->settings = new QSettings(QSettings::IniFormat, QSettings::UserScope,
                                   QApplication::organizationDomain(),QApplication::applicationName());
    this->errLabel = NULL;

    
}	


/**
* @brief show the given error in the dialog
*/
void ConnectToCoreDialog::setErrorString(QString err)
{
  if (!this->errLabel)
  {
    this->errLabel = new QLabel(this);
    this->form->verticalLayout_4->addWidget(this->errLabel);
  }
  this->errLabel->setText("ERROR: " + err);
}

/**
* @brief called whenever the User renamed an account
*
* If the user renamed an account entry, we have to update the settings
* file using QSettings. As the account name is used as a key group
* in the settingsfile we have to rewrite the group.
*
* @param item the account item that changed
*/
void ConnectToCoreDialog::itemChanged(QListWidgetItem* item)
{
    if (this->inEdit == "")
        return;
    qDebug() << "saved item name: " << this->inEdit;
    qDebug() << settings->childKeys();
    qDebug() << settings->childGroups();
    settings->beginGroup("accounts");
    qDebug() << settings->childKeys();
    settings->beginGroup(this->inEdit);
    QHash<QString,QString> t;
    QStringList keys = settings->childKeys();
    qDebug() << "keys: "  << keys;
    for (int j=0;j<keys.length();j++)
    {
        t.insert(keys.value(j),settings->value(keys.value(j)).toString());
        settings->remove(keys.value(j));
        qDebug() << "inner loop";
    }
    settings->endGroup();
    settings->beginGroup(item->text());
    for (int i=0;i<keys.length();i++)
    {
        settings->setValue(keys.value(i),t.value(keys.value(i)));
    }
    settings->endGroup();
    settings->endGroup();
}

/**
* @brief make the SslClient establish a connection with emitted data
*
* get the information from the form and initiate connection
*/
void ConnectToCoreDialog::doConnect()
{
    emit startConnect(this->form->hostnameEdit->text(),
                 this->form->portEdit->text(),
                 this->form->usernameEdit->text(),
                 this->form->passwordEdit->text());
}


/**
* @brief update flags, temporary vars and edit-fields
*
* Update the contents of the connection settings boxes whenever another 
* connection is selected.
*
* This is a temporary solution mostly. We set each item to be editable
* when it gets selected. This should better be one on insertion.
*
* Also, we update the variable inEdit which always holds the name of the
* account currently edited. This is used for updating the QSettings when
* the account name gets changed.
*/
void ConnectToCoreDialog::selectionChanged()
{
    QListWidgetItem* item = this->form->accountList->selectedItems().value(0);
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    this->inEdit = item->text();
    
    settings->beginGroup("accounts");
    settings->beginGroup(item->text());
    this->form->hostnameEdit->setText(settings->value("host").toString());
    this->form->portEdit->setText(settings->value("port").toString());
    this->form->usernameEdit->setText(settings->value("user").toString());
    this->form->passwordEdit->setText(settings->value("password").toString());
    settings->endGroup();
    settings->endGroup();
}
